//
//  YMSetPayPasswordViewController.swift
//  Driver
//
//  Created by lantaiyuan on 2017/10/27.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit

class YMSetPayPasswordViewController: UIViewController {
    @IBOutlet weak var tipsLb: UILabel!
    
    @IBOutlet weak var doneBtn: YMUIButton!
    
    @IBOutlet weak var passwordView: YMPasswordInputView!
    
    @IBAction func backAction(_ sender: UIBarButtonItem) {
        if isResetPay {
            guard
                let count = navigationController?.viewControllers.count,
                let vc = navigationController?.viewControllers[count - 3]
                else {
                    return
            }
            navigationController?.popToViewController(vc, animated: true)
            return
        }
        
        navigationController?.popViewController(animated: true)
    }
    
    fileprivate var isResetPay:Bool = false
    
    @IBAction func doneAction(_ sender: YMUIButton) {
        guard let first = firstPwd, let second = secondPwd else {
            return
        }
        
        if first != second {
            secondPwd = nil
            return
        }
        
        backAction(UIBarButtonItem())
    }
    
    fileprivate var firstPwd:String?
    fileprivate var secondPwd:String?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
                
        self.doneBtn.isHidden = true
        
        passwordView.finished = {[weak self](text)
            in

            if self?.firstPwd != nil && self?.secondPwd != nil {
                self?.doneAction(self?.doneBtn ?? YMUIButton())
            }else if self?.firstPwd != nil {
                self?.secondPwd = text
                self?.doneBtn.isHidden = false
            }else{
                self?.firstPwd = text
                self?.passwordView.clear()
                self?.tipsLb.text = "请再次输入以确认"
            }
        }
    }
    
    func comefromRestOldPassword(_ reset: Bool){
        isResetPay = reset
    }
}
